package com.djl.Zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

public class App {
    public static void main(String[] args) throws Exception {
        final CountDownLatch countDownLatch = new CountDownLatch(1);

       final ZooKeeper zooKeeper = new ZooKeeper("192.168.133.132:2181,192.168.133.133:2181,192.168.133.134:2181,192.168.133.135:2181",
                20000, new Watcher() {
            //回调方法
            public void process(WatchedEvent event) {
                Event.KeeperState state = event.getState();
                String path = event.getPath();
                Event.EventType type = event.getType();
                System.out.println("zk watcher");
                switch (state) {
                    case Disconnected:
                        break;
                    case SyncConnected:
                        System.out.println("connection");
                        countDownLatch.countDown();
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                    case Closed:
                        break;
                }
            }
        });

        countDownLatch.await();
        ZooKeeper.States state = zooKeeper.getState();
        switch (state) {
            case CONNECTING:
                System.out.println("connecting");
                break;
            case ASSOCIATING:
                break;
            case CONNECTED:
                System.out.println("connected");
                break;
            case CONNECTEDREADONLY:
                break;
            case CLOSED:
                break;
            case AUTH_FAILED:
                break;
            case NOT_CONNECTED:
                break;
        }

//        String nodePath = zooKeeper.create("/testNode", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//        System.out.println(nodePath);


        final Stat stat = new Stat();

        String testData = new String(zooKeeper.getData("/testNode", new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("get date() watcher");
                Event.KeeperState state = event.getState();
                String path = event.getPath();
                Event.EventType type = event.getType();
                try {
                    zooKeeper.getData("/testNode",this,stat);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, stat));
//
        Stat s = zooKeeper.setData("/testNode", "djlsd1".getBytes(), stat.getVersion());

        Stat s1 = zooKeeper.setData("/testNode", "djlsd12".getBytes(), stat.getVersion()+1);


//        System.out.println(s);
    }
}
